
define(["./events", "./dom", "./builder", "_"], function(events, dom, builder, _) {
  var SKIP_VALIDATION, clearSubmittingHidden, defaultValidateAndSubmit, exports, gatherParameters, setSubmittingHidden;
  SKIP_VALIDATION = "t5:skip-validation";
  clearSubmittingHidden = function(form) {
    var hidden;
    hidden = form.findFirst("[name='t:submit']");
    hidden && hidden.value(null);
    form.meta(SKIP_VALIDATION, null);
  };
  setSubmittingHidden = function(form, submitter) {
    var firstHidden, hidden, isCancel, mode, name;
    mode = submitter.attribute("data-submit-mode");
    isCancel = mode === "cancel";
    if (mode && mode !== "normal") {
      form.meta(SKIP_VALIDATION, true);
    }
    hidden = form.findFirst("[name='t:submit']");
    if (!hidden) {
      firstHidden = form.findFirst("input[type=hidden]");
      hidden = builder("input", {
        type: "hidden",
        name: "t:submit"
      });
      firstHidden.insertBefore(hidden);
    }
    name = isCancel ? "cancel" : submitter.element.name;
    hidden.value("[\"" + submitter.element.id + "\",\"" + name + "\"]");
  };
  gatherParameters = function(form) {
    var fields, result;
    result = {};
    fields = form.find("input, select, textarea");
    _.each(fields, function(field) {
      var existing, name, type, value;
      if (field.attribute("disabled")) {
        return;
      }
      type = field.element.type;
      if (type === "file" || type === "submit") {
        return;
      }
      value = field.value();
      if (value === null) {
        return;
      }
      name = field.element.name;
      existing = result[name];
      if (_.isArray(existing)) {
        existing.push(value);
        return;
      }
      if (existing) {
        result[name] = [existing, value];
        return;
      }
      return result[name] = value;
    });
    return result;
  };
  defaultValidateAndSubmit = function() {
    var field, memo, _i, _len, _ref;
    if (((this.attribute("data-validate")) === "submit") && (!this.meta(SKIP_VALIDATION))) {
      this.meta(SKIP_VALIDATION, null);
      memo = {
        error: false
      };
      _ref = this.find("[data-validation]");
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        field = _ref[_i];
        field.trigger(events.field.inputValidation, memo);
      }
      if (!memo.error) {
        this.trigger(events.form.validate, memo);
      }
      if (memo.error) {
        clearSubmittingHidden(this);
        return false;
      }
    }
    this.trigger(events.form.prepareForSubmit);
  };
  dom.onDocument("submit", "form", defaultValidateAndSubmit);
  dom.onDocument("click", "input[type=submit], input[type=image]", function() {
    setSubmittingHidden(dom(this.element.form), this);
  });
  dom.onDocument("click", "a[data-submit-mode]", function() {
    var form;
    form = this.findParent("form");
    if (!form) {
      console.error("Submitting link element not contained inside a form element.");
      return false;
    }
    setSubmittingHidden(form, this);
    if (form.trigger("submit")) {
      form.submit();
    }
    return false;
  });
  return exports = {
    gatherParameters: gatherParameters,
    setSubmittingElement: setSubmittingHidden,
    skipValidation: function(form) {
      return form.meta(SKIP_VALIDATION, true);
    }
  };
});
